package me.ywy.apt.mediaplayer;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String path = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/small.mp4";
    private Button mainPaly;
    private Button mainStop;
    private SurfaceView surfaceciew;
    private MediaPlayer mediaPlayer;
    private DNPlayer dnPlayer;
    private Button mainPauseStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();


        dnPlayer = new DNPlayer();
        dnPlayer.setDisplay(surfaceciew);
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestPermission();

    }

    private void requestPermission() {
        String[] request = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkWritePermission = ContextCompat.checkSelfPermission(this, request[0]);
            if (checkWritePermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(request, 1000);
            }
        }
    }

    private void initView() {
        mainPaly = findViewById(R.id.main_paly);
        mainStop = findViewById(R.id.main_stop);
        surfaceciew = findViewById(R.id.surfaceciew);
        mainPauseStart = findViewById(R.id.main_pause_start);

        mainPaly.setOnClickListener(this);
        mainStop.setOnClickListener(this);
        mainPauseStart.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_paly:
                playMedia();
                break;
            case R.id.main_stop:
                stopMedia();
                break;
            case R.id.main_pause_start:
                startOrPause();
                break;
        }
    }

    private boolean isPlaying;

    private void startOrPause() {
        if(mediaPlayer==null){
            return;
        }

        if(isPlaying){
            //暂停
            mediaPlayer.pause();
        }else{
            //继续
            mediaPlayer.start();
        }
    }

    private void stopMedia() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }

//        dnPlayer.stop();
    }

    private void playMedia() {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.setDisplay(surfaceciew.getHolder());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
            isPlaying = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

//        dnPlayer.play(path);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        dnPlayer.release();
    }
}
